# Nelson example descriptivs stats

load("nelson_data.RData")
attach(kkk)

(meanY <- tapply(kspeech, vidcdum, mean))
(meanM1 <- tapply(ifspeech, vidcdum, mean))
(meanM2 <- tapply(idisord, vidcdum, mean))
(sdY <- tapply(kspeech, vidcdum, sd))
(sdM1 <- tapply(ifspeech, vidcdum, sd))
(sdM2 <- tapply(idisord, vidcdum, sd))
(ateY <- weighted.mean(kspeech, vidcdum) - weighted.mean(kspeech, 1- vidcdum))
(ateM1 <- weighted.mean(ifspeech, vidcdum) - weighted.mean(ifspeech, 1-vidcdum))
(ateM2 <- weighted.mean(idisord, vidcdum) - weighted.mean(idisord, 1-vidcdum))
(ateseY <- sqrt(sdY[1]^2/sum(1-vidcdum) + sdY[2]^2/sum(vidcdum)))
(ateseM1 <- sqrt(sdM1[1]^2/sum(1-vidcdum) + sdM1[2]^2/sum(vidcdum)))
(ateseM2 <- sqrt(sdM2[1]^2/sum(1-vidcdum) + sdM2[2]^2/sum(vidcdum)))

cat("Sample Mean Outcome", meanY, "(", sdY, ")\n")
cat("Sample Mean FS Mediator", meanM1,"(", sdM1, ")\n")
cat("Sample Mean PO Mediator", meanM2,"(", sdM2, ")\n")
cat("ATE on Outcome", ateY, "(", ateseY, ")\n")
cat("ATE on FS Mediator", ateM1,"(", ateseM1, ")\n")
cat("ATE on PO Mediator", ateM2,"(", ateseM2, ")\n")

